package nccloud.rongq.bill.rongqvo.action;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import nc.vo.rongq.rongq.AggRongQVO;
import nccloud.framework.core.exception.ExceptionUtils;
import nccloud.framework.service.ServiceLocator;
import nccloud.framework.web.action.itf.ICommonAction;
import nccloud.framework.web.container.IRequest;
import nccloud.framework.web.ui.pattern.billcard.BillCardOperator;
import nccloud.itf.rongq.bill.IRongqvoMaintain;

public class GenerateVoucher implements ICommonAction {

	@Override
	public Object doAction(IRequest request) {
		
		String content = request.read();
		
		JSONObject root = JSONObject.parseObject(content);
		JSONArray pks = root.getJSONArray("pks");
		String redPz = null;
		if(root.containsKey("redPz")) {
			redPz = root.getString("redPz");
		}
		
		String[] ids = new String[pks.size()];
		for(int i = 0; i < ids.length; i ++) {
			ids[i] = pks.getString(i);
		}
		
		AggRongQVO[] bills = null;
		
		IRongqvoMaintain find = ServiceLocator.find(IRongqvoMaintain.class);
		try {
			
			bills = find.genVoucher(ids, redPz);
		} catch (nc.vo.pub.BusinessException e) {
			e.printStackTrace();
			ExceptionUtils.wrapBusinessException(e.getMessage());
		}
		
		BillCardOperator billCardOperator = new BillCardOperator();
		Object billcard = billCardOperator.toCard(bills[0]);
		return billcard;
	}

}
